package test3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Stack;

public class TestSort {
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int min=i;
            for (int j = i+1; j < array.length; j++) {
                if(array[min]>array[j]){
                    min=j;
                }
            }
            if(min!=i){
                int tmp=array[min];
                array[min]=array[i];
                array[i]=tmp;
            }
        }
    }
    public static void shiftDown(int[] array,int parent,int len){
        int child=parent*2+1;
        while (child<len){
            if(child+1<len&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                int tmp=array[child];
                array[child]=array[parent];
                array[parent]=tmp;
            }
            else {
                break;
            }
        }
    }
    public static void createBigHeap(int[] array){
        for (int i = (array.length-1-1)/2; i >=0; i--) {
            shiftDown(array,i,array.length);
        }
    }
    public static void heapSort(int[] array){
        createBigHeap(array);
        int end= array.length-1;
        while (end>0){
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void insertSort(int[] array) {
        if(array==null) return;
        for (int i = 0; i < array.length; i++) {
            int tmp=array[i];
            int j=i-1;
            for(;j>=0;j--){
                if(tmp<array[j]){
                    array[j+1]=array[j];
                }
                else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    public static void shell(int[] array,int gap) {
        if(array==null) return;
        for (int i = gap; i < array.length; i++) {
            int tmp=array[i];
            int j=i-gap;
            for(;j>=0;j--){
                if(tmp<array[j]){
                    array[j+gap]=array[j];
                }
                else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag=false;
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flag=true;
                }
            }
            if(flag==false){
                break;
            }
        }
    }
    public static void shellSort(int[] array){
        int length= array.length-1;
        while (length>0){
            shell(array,length);
            length=length/2;
        }
        shell(array,1);
    }
    public static void medianOfThree(int[] array,int left,int right) {
        int mid=(left+right)/2;
        if(array[mid] < array[left]) {
            int tmp = array[mid];
            array[mid] = array[left];
            array[left] = tmp;
        }//array[mid] <= array[start]
        if(array[left] > array[right]) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
        }//array[start] <= array[right]
        if(array[mid] > array[right]) {
            int tmp = array[mid];
            array[mid] = array[right];
            array[right] = tmp;
        }
    }
    public static int partition(int[] array, int start, int end){
        int tmp = array[start];
        while (start < end) {
            //1、先判断后面
            while (start < end && array[end] >= tmp) {
                end--;
            }
            //1.1 后面的给start  array[start] = array[end]
            array[start] = array[end];

            //2、再判断前边
            while (start < end && array[start] <= tmp) {
                start++;
            }
            //2.1 把这个大的给end  array[end] = array[start]
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }
    public static void quick(int[] array,int left,int right){
        if(left>=right) {
            return;
        }
        int pivot=partition(array,left,right);
        quick(array,left, pivot-1);
        quick(array,pivot+1, array.length-1);
    }
    public static void quickSort1(int[] array){
        quick(array,0, array.length-1);
    }
    public static void quickSort(int[] array){
        Stack<Integer> stack=new Stack<>();
        stack.push(0);
        stack.push(array.length-1);
        while (!stack.isEmpty()){
            int right =stack.pop();
            int left=stack.pop();
            if(left>=right){
                continue;
            }
            int pivot=partition(array,left,right);

            stack.push(left);
            stack.push(pivot-1);
            stack.push(pivot+1);
            stack.push(right);
        }
    }
    public static void merge(int[] array,int left,int right,int mid){
        PriorityQueue<Integer> queue=new PriorityQueue<>();
        ArrayList<Integer> a=new ArrayList<>();
        int[] tmp=new int[right-left+1];
        int k=0;
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        while (s1<=e1&&s2<=e2){
            if(array[s1]<=array[s2]){
                tmp[k++]=array[s1++];
            }
            else {
                tmp[k++]=array[s2++];
            }
        }
        while (s1<=e1){
            tmp[k++]=array[s1++];
        }
        while (s2<=e2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left]=tmp[i];
        }
    }
    public static void mergeSortInternal(int[] array,int left,int right){
        if (left>=right){
            return;
        }
        int mid=(left+right)/2;
        mergeSortInternal(array,left,mid);
        mergeSortInternal(array,mid+1,right);
        merge(array,left,right,mid);
    }
    public static void mergeSort1(int[] array){
        mergeSortInternal(array,0,array.length-1);
    }
    public static void merge2(int[] array,int gap){
        int[] tmp=new int[array.length];
        int k=0;
        int s1=0;
        int e1=s1+gap-1;
        int s2=e1+1;
        int e2=s2+gap-1>=array.length?array.length-1:s2+gap-1;
        while (s2<array.length){
            while (s1<=e1&&s2<=e2){
                if (array[s1]<=array[s2]){
                    tmp[k++]=array[s1++];
                }
                else {
                    tmp[k++]=array[s2++];
                }
            }
            while (s1<=e1){
                tmp[k++]=array[s1++];
            }
            while (s2<=e2){
                tmp[k++]=array[s2++];
            }
            s1=e2+1;
            e1=s1+gap-1;
            s2=e1+1;
            e2=s2+gap-1>=array.length?array.length-1:s2+gap-1;
        }
        while (s1<=array.length-1){
            tmp[k++]=array[s1++];
        }
        for (int i=0;i<array.length;i++) {
            array[i]=tmp[i];
        }
    }
    public static void mergeSort(int[] array){
        for(int gap=1;gap<array.length;gap*=2){
            merge2(array,gap);
        }
    }
    public static void main(String[] args) {
        int[] array = {6,2,1,7,5,5,8,9,9,5,8,6,5,6,55,856,2};
        System.out.println(Arrays.toString(array));
        mergeSort(array);
        System.out.println(Arrays.toString(array));
    }
}
